package com.agileboot.admin.controller.airport;

import com.agileboot.admin.customize.aop.accessLog.AccessLog;
import com.agileboot.common.core.base.BaseController;
import com.agileboot.common.core.dto.ResponseDTO;
import com.agileboot.common.core.page.PageDTO;
import com.agileboot.common.enums.common.BusinessTypeEnum;
import com.agileboot.common.utils.poi.CustomExcelUtil;
import com.agileboot.domain.airport.reformDetail.db.SirReformDetailEntity;
import com.agileboot.domain.common.command.BulkOperationCommand;
import com.agileboot.domain.airport.reform.ReformMainApplicationService;
import com.agileboot.domain.airport.reform.command.AddReformMainCommand;
import com.agileboot.domain.airport.reform.command.UpdateReformMainCommand;
import com.agileboot.domain.airport.reform.dto.ReformMainDTO;
import com.agileboot.domain.airport.reform.query.ReformMainQuery;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

@Tag(name = "整改通知书API", description = "整改通知书相关的增删查改")
@RestController
@RequestMapping("/airport/reformMain")
@Validated
@RequiredArgsConstructor
public class ReformMainController extends BaseController {

    private final ReformMainApplicationService applicationService;

    @Operation(summary = "整改通知书分页查询")
    //     @PreAuthorize("@permission.has('airport:reformMain:list')")
    @GetMapping("/list")
    public ResponseDTO<PageDTO<ReformMainDTO>> list(ReformMainQuery query) {
        return ResponseDTO.ok(applicationService.getReformList(query));
    }

    @Operation(summary = "整改通知书导出Excel")
    @AccessLog(title = "整改通知书", businessType = BusinessTypeEnum.EXPORT)
    //     @PreAuthorize("@permission.has('airport:reformMain:export')")
    @GetMapping("/excel")
    public void export(HttpServletResponse response, ReformMainQuery query) {
        List<ReformMainDTO> list = applicationService.getReformListAll(query);
        CustomExcelUtil.writeToResponse(list, ReformMainDTO.class, response);
    }

    @Operation(summary = "整改通知书详情")
    //     @PreAuthorize("@permission.has('airport:reformMain:query')")
    @GetMapping("/{reformId}")
    public ResponseDTO<ReformMainDTO> getInfo(@PathVariable Long reformId) {
        return ResponseDTO.ok(applicationService.getReformInfo(reformId));
    }

    @Operation(summary = "新增整改通知书")
    //     @PreAuthorize("@permission.has('airport:reformMain:add')")
    @AccessLog(title = "整改通知书", businessType = BusinessTypeEnum.ADD)
    @PostMapping
    public ResponseDTO<Void> add(@RequestBody AddReformMainCommand command) {
        applicationService.addReform(command);
        return ResponseDTO.ok();
    }

    @Operation(summary = "修改整改通知书")
    //     @PreAuthorize("@permission.has('airport:reformMain:edit')")
    @AccessLog(title = "整改通知书", businessType = BusinessTypeEnum.MODIFY)
    @PutMapping
    public ResponseDTO<Void> edit(@RequestBody UpdateReformMainCommand command) {
        applicationService.updateReform(command);
        return ResponseDTO.ok();
    }

    @Operation(summary = "删除整改通知书")
    //     @PreAuthorize("@permission.has('airport:reformMain:remove')")
    @AccessLog(title = "整改通知书", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping
    public ResponseDTO<Void> remove(@RequestParam @NotNull @NotEmpty List<Long> ids) {
        applicationService.deleteReform(new BulkOperationCommand<>(ids));
        return ResponseDTO.ok();
    }


    /**
     * 根据整改ID查询整改详情
     */
    @Operation(summary = "根据整改ID查询整改详情")
    @GetMapping("/reformDetail/{reformId}")
    public ResponseDTO<SirReformDetailEntity> getReformDetail(@PathVariable Long reformId) {
        SirReformDetailEntity reformDetail = applicationService.getReformDetail(reformId);
        return ResponseDTO.ok(reformDetail);
    }


}
